package com.tepth.busfix.main.repair;

import android.app.Activity;
import android.app.Dialog;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.tepth.busfix.R;
import com.tepth.busfix.bean.reponse.BusInfo;
import com.tepth.busfix.bean.reponse.Process;
import com.tepth.busfix.bean.reponse.ProcessDecorate;
import com.tepth.busfix.bean.reponse.Trouble;
import com.tepth.busfix.bean.reponse.TroubleContent;
import com.tepth.busfix.main.alarm.AlarmInfoActivity;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.List;

/**
 * Created by zhangyu on 2017/7/13.
 */

public class SelectOperatorDialog extends Dialog {
    private OnOperatorAddListener mListenerToAlarm;
    private OnOperatorSearchListener mListenerAdd;
    private BusInfo mBusInfo;
    private Trouble mTrouble;

    public SelectOperatorDialog(@NonNull final Activity context,
                                BusInfo busInfo, final List<Process> list
            , final Trouble trouble, OnOperatorAddListener listener, OnOperatorSearchListener clickListener) {
        super(context);
        mListenerToAlarm = listener;
        mListenerAdd = clickListener;
        mBusInfo = busInfo;
        mTrouble = trouble;
        setTitle(context.getResources().getString(R.string.common_deal_way));
        View view = LayoutInflater.from(context).
                inflate(R.layout.dialog_select_operator, null);
        TextView tvEmpty = (TextView) view.findViewById(R.id.tv_empty);
        final ListView listView = (ListView) view.findViewById(R.id.list_view);
        if (list == null || list.isEmpty()) {
            tvEmpty.setVisibility(View.VISIBLE);
            listView.setVisibility(View.GONE);
        } else {
            tvEmpty.setVisibility(View.GONE);
            listView.setVisibility(View.VISIBLE);
            listView.setAdapter(new CommonAdapter<Process>(context, R.layout.item_select_operator, list) {
                @Override
                protected void convert(ViewHolder viewHolder, Process item, int position) {
                    TroubleContent.Part part = item.getAccessory() == null ?
                            item.getComponent() : item.getAccessory();
                    String stringBuilder = item.getOperation().getName() +
                            item.getUseCount() +
                            part.getUnit() +
                            item.getName();
                    viewHolder.setText(R.id.tv_des, stringBuilder);
                    viewHolder.setText(R.id.tv_percent, item.getPercentage() + "%");
                }
            });
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Process process = list.get(position);
                    ProcessDecorate processDecorate =
                            new ProcessDecorate(mTrouble.getClassification().getId(),
                                    mTrouble.getId(),
                                    mTrouble.getTroubleTypeId(),
                                    mTrouble.getName(),
                                    mTrouble.getClassification().getName(), process);
                    mListenerToAlarm.onAddOperator(processDecorate);
                    dismiss();
                }
            });
        }
        view.findViewById(R.id.btn_check).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlarmInfoActivity.startActivity(context, mBusInfo, mTrouble.getTroubleTypeId(), true);
            }
        });
        view.findViewById(R.id.btn_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                mListenerAdd.onSearchOperator(trouble);
            }
        });
        setContentView(view);
    }

    interface OnOperatorAddListener {
        void onAddOperator(ProcessDecorate processDecorate);
    }

    interface OnOperatorSearchListener {
        void onSearchOperator(Trouble trouble);
    }
}
